package parser;

/**
 *@Author	Eric
 *@Date		27th May 2009
 *
 * Notitie: kan nog niet omgaan met AND rules!!
 */

import parser.Universe;
import parser.Variable;
import java.io.*;
import java.util.Scanner;
import java.util.Vector;

public class kb_parser {
	
	Vector UniverseVector = new Vector();		/* <universe list> */
	Vector VariableVector = new Vector();		/* <variable list> */
	Vector RuleVector = new Vector();		/* <rule list> */
	
	/* constructor */
	public kb_parser() {}
	
    public void Parse(String textfile) throws IOException
    {
    	
    	Scanner s = null;
        String word1,word2,word3,word4;			/* om 4 gescande woorden tegelijk te kunnen gebruiken */
        double set1,set2,set3,set4;				/* om 4 gescande woorden als doubles te kunnen gebruiken */
    	
        try {
        	s = new Scanner(new BufferedReader(new FileReader(textfile)));

        	while (s.hasNext()) {
                word1 = s.next();

                if(word1.equals("UNIVERSE"))
            	{
            		word2 = s.next();			/* lees <universe name> */
            		UniverseVector.add(new Universe(word2));
            	} else if(word1.equals("SET"))
            	{
            		word2 = s.next();			/* lees <set name> */
            		((Universe)UniverseVector.lastElement()).addSet(word2);
            		
            		// lees de 4 setvalues per SET (v.e. Universe)
            		set1 = Double.parseDouble(s.next());
            		set2 = Double.parseDouble(s.next());
            		set3 = Double.parseDouble(s.next());
            		set4 = Double.parseDouble(s.next());
            		double[] array = {set1,set2,set3,set4};
            		((Universe)UniverseVector.lastElement()).addSetValues(array);
            	} else if(word1.equals("VARIABLE"))
            	{
            		word2 = s.next();			/* lees <variable name> */
            		word3 = s.next();			/* lees <universe name> */
            		word4 = s.next();			/* lees <variable type> */
            		VariableVector.add(new Variable(word2,word3,word4));
            	} else if (word1.equals("RULE"))
            	{
               		word2 = s.next();			/* lees <rule name> */
            		RuleVector.add(new Rule(word2));
            	} else if(word1.equals("IF"))
            	{
            		/* N.b. we gaan er vanuit dat een <premisse> is van de vorm
            		 * IF <expressie>-deel IS <expressie>-deel
            		 * De <premisse> zou langer kunnen zijn..
            		 */
            		word2 = s.next(); s.next();		/* lees <expression>-deel na "IF" */
            		word3 = s.next();			/* lees <expression>-deel na "IS" */
            		String[] array = {word2,word3}; 
            		((Rule)RuleVector.lastElement()).addCondition(array);
            	} else if(word1.equals("THEN"))
            	{
            		/* N.b. we gaan er vanuit dat een <conclusion> is van de vorm
            		 * IF <expressie>-deel IS <expressie>-deel
            		 * De <premisse> zou langer kunnen zijn..
            		 */
            		word2 = s.next(); s.next();		/* lees <conclusion>-deel na "THEN" */
            		word3 = s.next();			/* lees <conclusion>-deel na "IS" */
            		String[] array = {word2,word3}; 
            		((Rule)RuleVector.lastElement()).addConclusion(array);
            	} //anders overbodig woord wat niet opgeslagen hoeft..
            }
        } finally {
            if (s != null) {
                s.close();
            }
        }
    }

    public Vector getUniverseVector()
    {
    	return this.UniverseVector;
    }
    
    public Vector getVariableVector()
    {
    	return this.VariableVector;
    }
    
    public Vector getRuleVector()
    {
    	return this.RuleVector;
    }
}